package com.chwn.curiousdiary.ui.other;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.ui.login.LoginActivity;
import com.chwn.curiousdiary.utils.SPUtils;


/**
 * Created by Myshikin on 2017/2/24.
 */

public class GuideActivity extends AppCompatActivity {

    private ViewPager mGuidePagers;

    private int[] pagersImgId = {R.drawable.guide_page_one, R.drawable.guide_page_two,
            R.drawable.guide_page_three};

    @Override
    protected void onCreate(Bundle savedInstanced) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanced);
        setContentView(R.layout.activity_guide);
        initView();
        initListener();
    }

    protected void initView() {
        mGuidePagers = (ViewPager) findViewById(R.id.vp_guide_pager);
        mGuidePagers.setAdapter(new GuidePagerAdapter());

    }

    protected void initListener() {
        mGuidePagers.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    class GuidePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pagersImgId.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            ImageView imageView = new ImageView(GuideActivity.this);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(layoutParams);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setBackgroundResource(pagersImgId[position]);
            container.addView(imageView);
            if (position == pagersImgId.length - 1) {
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SPUtils.saveBooleanToSP("ISFIRSTINGOAPP", true);
                        Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
            }
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
